<?php

//-------------- Load the paths ------------------------------------------------
include_once('paths.php');
//-------------- Load the framework --------------------------------------------
include_once(FRAMEWORK_DIR.'/class/Application.php');

class MyApplication extends FRM_Application
{
	function __construct()
	{
		parent::__construct();
		$this->addHook('Config','loadConfig');
		$this->addHook('Log','myLogs');
		$this->addHook('Request','loadBO',self::HOOK_AFTER);	
		$this->addHook('TemplateEngine','myTemplateEngine');
	}

	public function loadConfig()
	{
//		Many ways to do a configuration for an appli,
//		here is a simple yet flexible one
		FRM_Config::loadConfig(APPLICATION_DIR.'/config/url.php');
		include(APPLICATION_DIR.'/config/common.php');
		
	}

	public function myLogs()
	{
		$logger = FRM::load('logger');
//		Adding firebug and file loggers, always useful
		//$logger->addLogger('firebug');
		$logger->addLogger('file',APPLICATION_DIR.'/logs/errors.log');
	}
	
	public function loadBO()
	{			
		$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];		
		
		$url = FRM_Config::get('URL','ADMIN_BO');
		$package = 'Admin';
		
		FRM_Config::set('APPLICATION','URL',$url);
		FRM_Config::set('APPLICATION','NAME',$package); 
		FRM::register('package',$package);
		
		$contr = FRM_Request::getController();
		if (strpos($contr,"FRM_") === false)
		{
			FRM_Request::setController($package.'_'.($contr?$contr:FRM_Config::get('APPLICATION','DEFAULT_CONTROLLER')));
		}


	}
	
	public function myTemplateEngine()
	{
		$smarty = FRM::load("smarty");
		$smarty->left_delimiter =  '<{';
		$smarty->right_delimiter =  '}>'; 
		
		//self::loadMenuHighlighter();
	}
	
	/**
	 * find out which menu will be highlighted
	 *
	 */
	private function loadMenuHighlighter()
	{
		// :TODO: pass the behaviour of running something to the request
		$controller = FRM_String::capitalizeSymbol(FRM_Request::getController());
		if (!$controller)
		{
			$controller = FRM_Config::get('APPLICATION','DEFAULT_CONTROLLER');
		}
				
		$ControllerName = $controller."Controller";

		$curMenuItem = '';
		if (class_exists($ControllerName))
		{
			$class = new ReflectionClass($ControllerName);
			$curMenuItem = $class->getConstant('MENU_ITEM');
		}

		$smarty = FRM::load("smarty");
		$smarty->assign('menu', $curMenuItem);
	}
}
